from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.
from django.db.models import Avg
from utils.constants import USER_TYPE_CHOICES, USER_GENDER_CHOICES, QINIU_DOMAIN, AVATAR_MAN, AVATAR_WOMAN
from utils.models import BaseModel


class User(AbstractUser, BaseModel):
    """自定义用户模型类"""
    mobile = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    birthday = models.DateField(verbose_name="生日", null=True)
    desc = models.TextField(verbose_name="个人简介", default="这个人很懒，暂未留下任何讯息。")
    avatar = models.CharField(max_length=200, verbose_name="头像", default="")
    type = models.IntegerField(verbose_name="类型", choices=USER_TYPE_CHOICES, default=0)
    sex = models.IntegerField(verbose_name="性别", choices=USER_GENDER_CHOICES, default=0)
    fans = models.ManyToManyField('self', related_name='following_users', verbose_name='粉丝', symmetrical=False)

    class Meta:
        db_table = 'tb_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

    @property
    def decorate_avatar(self):
        if self.avatar == '':
            if self.sex == 0:
                return QINIU_DOMAIN + AVATAR_MAN
            else:
                return QINIU_DOMAIN + AVATAR_WOMAN
        else:
            return QINIU_DOMAIN + self.avatar

    @property
    def teacher_id(self):
        return self.teacher.id

    @property
    def fans_count(self):
        return self.fans.count()

    @property
    def following_user_count(self):
        return self.following_users.count()

    @property
    def collection_articles_count(self):
        return self.collection_articles.count()


class Teacher(BaseModel):
    real_name = models.CharField(max_length=4, verbose_name="真实姓名")
    introduction = models.TextField(verbose_name="简介")
    education = models.CharField(max_length=20, verbose_name="学历")
    university = models.CharField(max_length=20, verbose_name="高校")
    major = models.CharField(max_length=20, verbose_name="专业")
    salary = models.DecimalField(verbose_name="价格", max_digits=5, decimal_places=2, null=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="用户", related_name="teacher")
    city = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='city', verbose_name='市')
    province = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='province', verbose_name='省')
    like_users = models.ManyToManyField(User, verbose_name='喜欢该老师的用户', related_name='like_teachers')
    fans = models.ManyToManyField(User, verbose_name='粉丝', related_name='following_teachers')

    @property
    def like_count(self):
        return self.like_users.count()

    @property
    def rating_avg(self):
        avg = self.comments.aggregate(Avg('rating'))
        avg = avg.get('rating__avg', None)
        if not avg:
            avg = 3
        return avg

    class Meta:
        db_table = 'tb_teachers'
        verbose_name = '老师'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.real_name


class SearchHistory(BaseModel):
    query = models.CharField(max_length=200, verbose_name='搜索内容')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='search_history', verbose_name='用户')


class ChatClients(BaseModel):
    channel_name = models.CharField(max_length=200)
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='chat_client', unique=True)

    class Meta:
        db_table = 'tb_chat_clients'
        verbose_name = '通信实例'
        verbose_name_plural = verbose_name


class ChatMessage(BaseModel):
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sent_message', verbose_name='发送方',
                               null=True)
    receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='received_message', verbose_name='接收方')
    message = models.TextField(verbose_name='信息')
    read = models.BooleanField(default=False, verbose_name='是否已读')
    is_system = models.BooleanField(default=False, verbose_name='是否是系统信息')

    class Meta:
        db_table = 'tb_chat_message'
        verbose_name = '通信信息'
        verbose_name_plural = verbose_name
